package com.example.questionnaire.controller;


import com.example.questionnaire.common.ResponseCode;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.LoginPageService;
import io.swagger.annotations.Api;
import com.example.questionnaire.common.ServerResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/**
 * 首页
 */
@RestController
@RequestMapping("/LoginPage")
@Api(tags = "登录首页")
public class LoginPageController {

    @Autowired
    private LoginPageService loginPageService;


    /**
     * 微信登录
     * @param code（编码）
     */
    @PostMapping("/wxLogin")
    @ApiOperation(value = "微信登录", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse wxLogin(@RequestParam String code) throws IOException {

        return ServerResponse.createBySuccess(loginPageService.wxLogin(code));

    }

    /**
     * 手机号注册
     * @param phone（手机号）
     * @param password（密码）
     */
    @PostMapping("/phoneEnroll")
    @ApiOperation(value = "手机号注册", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public Map<String ,Object> phoneEnroll(@RequestParam String phone, @RequestParam String password) throws IOException {
// //判断手机号格式是否正确
// if (!isValidphone(phone)) {
// return ServerResponse.createByError("注册失败，手机号不正确").toMap();
// }
//执行注册逻辑

        ServerResponse server = loginPageService.phoneEnroll(phone, password);
        if (server.getCode() == ResponseCode.ResponseCode1.SUCCESS){
            return ServerResponse.createBySuccess("注册成功").toMap();
        }else if (server.getCode() == ResponseCode.ResponseCode1.USER_EXIST){
            return ServerResponse.createByUserExistError("注册失败，已有用户").toMap();
        }else {
            return ServerResponse.createByError("注册失败").toMap();
        }
    }
// private boolean isValidphone(String phone) {
// // 使用正则表达式验证手机号格式
//// String pattern = "^1[0-9]{10}$";
// String pattern = "^1[34578]\\d{9}$";
// return Pattern.matches(pattern, phone);
// }

    /**
     * 手机号登录
     * @param phone（手机号）
     * @param password（密码）
     */
    @PostMapping("/phoneLogin")
    @ApiOperation(value = "手机号登录", response = ServerResponse.class, httpMethod = "POST")
    public ServerResponse phoneLogin(@RequestParam String phone, @RequestParam String password) throws IOException {

        return ServerResponse.createBySuccess(loginPageService.phoneLogin(phone,password));

    }






}
